package com.example.springboot_demo.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.example.springboot_demo.pojo.*;
import org.springframework.beans.BeanUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

public class CrmTest {

    public static void main(String[] args) {

        ArrayList<Store> storeList = new ArrayList<>();
        try {
            EasyExcel.read(new File("/Users/jiangan/Desktop/store.xlsx"), Store.class, new AnalysisEventListener<Store>() {

                @Override
                public void invoke(Store batchWxActInfo, AnalysisContext analysisContext) {
                    storeList.add(batchWxActInfo);
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                }
            }).doReadAll();
        } catch (Exception e) {
            e.printStackTrace();
        }

        ArrayList<OrderInfo> orderList = new ArrayList<>();
        try {
            EasyExcel.read(new File("/Users/jiangan/Desktop/database.xlsx"), OrderInfo.class, new AnalysisEventListener<OrderInfo>() {

                @Override
                public void invoke(OrderInfo batchWxActInfo, AnalysisContext analysisContext) {
                    orderList.add(batchWxActInfo);
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                }
            }).doReadAll();
        } catch (Exception e) {
            e.printStackTrace();
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");

        List<Store> stores = storeList.stream().map(o -> {
            try {
                Date parse = sdf.parse(o.getCtime());
                o.setStartDate(parse);
            } catch (ParseException e) {
            }
            return o;
        }).collect(Collectors.toList());


        Map<String, List<OrderInfo>> map = orderList.stream().map(o-> {
            try {
                Date parse = sdf.parse(o.getMtime());
                o.setOrderDate(parse);
            } catch (ParseException e) {
            }
            return o;
        } ). collect(Collectors.groupingBy(o -> o.getStoreId()));
        for (Map.Entry<String, List<OrderInfo>> entry : map.entrySet()) {
            List<OrderInfo> value = entry.getValue();
            value.sort(Comparator.comparing(o->o.getOrderDate()));
        }

        for (Store store : stores) {
            List<OrderInfo> orderInfos = map.get(store.getStoreId());
            long count = 0;
            long begin =  store.getStartDate().getTime() - 86400000L;
            long after =  store.getStartDate().getTime() + 518400000L;
            long after30 =  store.getStartDate().getTime() + 2505600000L;
            boolean flag = false;
            for (OrderInfo orderInfo : orderInfos) {
                if (orderInfo.getOrderDate().getTime() > begin) {
                    count += orderInfo.getOrderCount();
                    if ( orderInfo.getOrderDate().getTime() < after) {
                        if (count >= 3 && store.getSuccess()==null) {
                            store.setSuccess(orderInfo.getMtime());
                            flag = true;
                        }
                    }
                    if (flag && orderInfo.getOrderDate().getTime() < after30) {
                        if (count >= 100) {
                            store.setEffect(orderInfo.getMtime());
                            break;
                        }
                    }

                }

            }
        }


        List<StoreExport> collect = stores.stream().map(o -> {
            StoreExport storeExport = new StoreExport();
            BeanUtils.copyProperties(o, storeExport);
            return storeExport;
        }).collect(Collectors.toList());

        for (Store store : stores) {
            if (store.getEffect() != null ) {
                //两个时间都有
                String sql = "UPDATE jjz_crm_keeper_activate_store_record SET `status` = 2, activate_time = null, effective_time = '" + store.getEffect() + "' WHERE id =" + store.getId() + " and store_id = '" + store.getStoreId() + "' ;";
                System.out.println(sql);
            } else if (store.getSuccess() != null) {
                //只有一个时间
                String sql = "UPDATE jjz_crm_keeper_activate_store_record SET `status` = 1, activate_time = '" + store.getSuccess() + "' WHERE id =" + store.getId() + " and store_id = '" + store.getStoreId() + "' ;";
                System.out.println(sql);
            }
        }

        EasyExcel.write(new File("/Users/jiangan/Desktop/export.xlsx"), StoreExport.class).sheet("sheet1").doWrite(collect);

    }
}
